package com.yulongtian.oneProblemEveryDay.month202302;

/**
 * https://leetcode.cn/problems/evaluate-boolean-binary-tree/
 * 可
 *
 * @author yulongTian
 * @create 2023-02-06 10:50
 */
public class Test04 {
    public static void main(String[] args) {

    }


    public boolean evaluateTree(TreeNode root) {
        return dfs(root) == 1;
    }

    //后序遍历
    public int dfs(TreeNode root) {
        if (root == null) {
            return -1;
        }
        //左
        int left = dfs(root.left);
        // 右
        int right = dfs(root.right);

        if (left == -1 && right == -1) {
            return root.val;
        } else {
            // 根
            if (root.val == 2) {
                return left | right;
            } else {
                return left & right;
            }
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
